home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / monitory / lav / load.c < prev    next >
C/C++ Source or Header  |  1993-06-14  |  5KB  |  217 lines

  1. #include <exec/exec.h>
  2. #include <intuition/intuition.h>
  3. #include <intuition/classes.h>
  4. #include <intuition/classusr.h>
  5. #include <intuition/imageclass.h>
  6. #include <intuition/gadgetclass.h>
  7. #include <libraries/gadtools.h>
  8. #include <graphics/displayinfo.h>
  9. #include <graphics/gfxbase.h>
  10. #include <clib/exec_protos.h>
  11. #include <clib/intuition_protos.h>
  12. #include <clib/gadtools_protos.h>
  13. #include <clib/graphics_protos.h>
  14. #include <clib/utility_protos.h>
  15. #include <string.h>
  16. #include <clib/diskfont_protos.h>
  17.  
  18.  
  19. #include "lavd.h"
  20. #include "ALoad.h"
  21. #include "timer.h"
  22.  
  23. static const char    PortName[] = "lavd";
  24. struct MsgPort    *lavdPort = NULL;
  25. struct MsgPort    *RepPort = NULL;    /* Reply port. */
  26. lavdMsg    Req;
  27.  
  28. int
  29. ALoadCloseWindow()
  30. {
  31.     return 0;
  32. }
  33.  
  34. int
  35. GetLoad()
  36. {
  37.     /* Forbid so the port doesn't go away... */
  38.     Forbid();
  39.     if ((lavdPort = FindPort(PortName)) == NULL) {
  40.         Permit();
  41.         PutStr("Demon not running.\n");
  42.         return 0;
  43.     }
  44.     PutMsg (lavdPort, &Req);
  45.     Permit();
  46.     
  47.     WaitPort(RepPort);    /* Wait for the reply. */
  48.  
  49.     return;
  50. }
  51.  
  52. void
  53. Main()
  54. {
  55.     int    Stop = 0;
  56.     register int    Left, Right, Top, Bottom;    /* Size of region to render into. */
  57.     int    Width, Height;
  58.     int    *Loads;    /* Values of each column on the screen. */
  59.     ULONG    Sig;
  60.     int    LoadPos = 0;    /* Position within the array. */
  61.     int    MaxLoad, MaxPos = -1;    /* The largest load, and its' position in the array. */
  62.     int    TopLoad;    /* The top of the graph. */
  63.     int    H;
  64.     int    Max = Loads [0];
  65.     int    Pos = 0;
  66.     int    j = 0, i;
  67.     int    OldTopLoad;
  68.  
  69.     Req.lm_Message.mn_ReplyPort = RepPort;
  70.     Req.lm_Message.mn_Length = sizeof(lavdMsg);
  71.     Req.Class = LAV_LOAD;
  72.  
  73.     Left =    ALoadWnd->BorderLeft + 1;
  74.     Right = ALoadWnd->Width - ALoadWnd->BorderRight - 1;
  75.     Top = ALoadWnd->BorderTop + 1;
  76.     Bottom = ALoadWnd->Height - ALoadWnd->BorderBottom - 1;
  77.  
  78.     Width = Right - Left;
  79.     Height = Bottom - Top;
  80.  
  81.     if ((Loads = (int *) calloc (Width, sizeof(int))) == NULL){
  82.         PutStr ("Out of memory.\n");
  83.         return;
  84.     }
  85.  
  86.     if (!GetLoad()){
  87.         return;
  88.     }
  89.  
  90.     TopLoad = Req.L1>100?(Req.L1 / 100 + 1) * 100:100;
  91.     MaxLoad = Req.L1;
  92.     MaxPos = -1;
  93.  
  94.     while (! Stop){
  95.         timer_start(5000000);
  96.         Sig = Wait(1L<<ALoadWnd->UserPort->mp_SigBit | 1L<<timer_port->mp_SigBit);
  97.         if (Sig & 1L<<ALoadWnd->UserPort->mp_SigBit){
  98.             if (HandleALoadIDCMP() == 0)
  99.                 Stop = 1;
  100.         }
  101.         if (Sig & 1L<<timer_port->mp_SigBit){
  102.             if (!GetLoad()){
  103.                 return;
  104.             }
  105.     
  106.             WaitPort(RepPort);    /* Wait for the reply. */
  107.  
  108.             Loads[LoadPos] = Req.L1;
  109.  
  110.             if (Req.L1 > MaxLoad || MaxPos == LoadPos){
  111.                 OldTopLoad = TopLoad;
  112.                 if (MaxPos == LoadPos){    /* The peak has gone off the
  113.                              * left edge - rescale. */
  114.                     Max = 0;
  115.                     for (i = 0; i < Width; i++){
  116.                         if (Max < Loads[i]){
  117.                             Max = Loads[i];
  118.                             Pos = i;
  119.                         }
  120.                     }
  121.                     MaxLoad = Max;
  122.                     Max = (Max / 100 + 1) * 100;
  123.                     MaxPos = Pos;
  124.                     TopLoad = Max;
  125.                 } else {
  126.                     TopLoad = (Req.L1 / 100 + 1) * 100;
  127.                     MaxLoad = Req.L1;
  128.                     MaxPos = LoadPos;
  129.                 }
  130.                 if (TopLoad != OldTopLoad){
  131.                     SetAPen (ALoadWnd->RPort, 0);
  132.                     RectFill (ALoadWnd->RPort, Left, Top, Right, Bottom);
  133.                     SetAPen (ALoadWnd->RPort, 3);
  134.  
  135.                     j = 1;
  136.                     for (i = LoadPos + 1; i != LoadPos;){
  137.                         if (Loads[i] != 0){
  138.                             Move(ALoadWnd->RPort, j + Left + 1, Bottom);
  139.                             H = Top + ((Height * (TopLoad - Loads[i])) / TopLoad);
  140.                             Draw(ALoadWnd->RPort, j + Left + 1, H);
  141.                         }
  142.                         j++;
  143.                         i++;
  144.                         if (i == Width){
  145.                             i = 0;
  146.                         }
  147.                     }
  148.                     if (TopLoad > 100){
  149.                         SetAPen (ALoadWnd->RPort, 1);
  150.                         for (i = 100; i < TopLoad; i+=100){
  151.                             H = Top + ((Height * (TopLoad - i)) / TopLoad);
  152.                             Move(ALoadWnd->RPort, Left, H);
  153.                             Draw(ALoadWnd->RPort, Right, H);
  154.                         }
  155.                         SetAPen (ALoadWnd->RPort, 3);
  156.                     }
  157.                 }
  158.             }
  159.             /* Now display it. */
  160.             ScrollRaster(ALoadWnd->RPort, 1, 0, Left, Top, Right, Bottom);
  161.             Move(ALoadWnd->RPort, Right, Bottom);
  162.             H = Top + ((Height * (TopLoad - Req.L1)) / TopLoad);
  163.             Draw(ALoadWnd->RPort, Right, H);
  164.             if (TopLoad > 100){
  165.                 SetAPen(ALoadWnd->RPort, 1);
  166.                 for (i = 100; i < TopLoad; i+=100){
  167.                     H = Top + ((Height * (TopLoad - i)) / TopLoad);
  168.                     WritePixel(ALoadWnd->RPort, Right, H);
  169.                 }
  170.                 SetAPen(ALoadWnd->RPort, 3);
  171.             }
  172.  
  173.             LoadPos++;
  174.             if (LoadPos == Width){
  175.                 LoadPos = 0;
  176.             }
  177.         }
  178.     }
  179. }
  180.  
  181. main()
  182. {
  183.  
  184.     if (!timer_open()){
  185.         PutStr("Couldn't open timer.\n");
  186.         goto exitmain;
  187.     }
  188.     if (SetupScreen() != 0){
  189.         PutStr("Couldn't lock screen.\n");
  190.         goto exitmain;
  191.     }
  192.     if (OpenALoadWindow() != 0){
  193.         PutStr("Couldn't open window.\n");
  194.         goto exitmain;
  195.     }
  196.  
  197.     if ((RepPort = CreateMsgPort()) == NULL) {
  198.         PutStr("Couldn't create a message port.\n");
  199.         goto exitmain;
  200.     }
  201.  
  202.     SetAPen (ALoadWnd->RPort, 3);
  203.  
  204.     Main();
  205.     
  206.       exitmain:
  207.     CloseALoadWindow();
  208.     CloseDownScreen();
  209.     if (RepPort != NULL) {
  210.         DeleteMsgPort(RepPort);
  211.     }
  212.     if (!timer_test())
  213.         timer_abort();
  214.     timer_close();
  215.     exit(0);
  216. }
  217.